package {
	import mx.collections.ArrayCollection;
	import spark.events.DropDownEvent;
	import spark.events.TextOperationEvent;
	import flash.events.FocusEvent;
	import spark.components.ComboBox;
	import spark.components.DropDownList;
	import mx.events.CollectionEvent;
	import mx.core.mx_internal;
	import mx.core.ClassFactory;
	import flash.utils.setTimeout;
	import mx.collections.IList;
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import  mx.managers.FocusManager;	
	use namespace mx_internal;


	public class CheckCombo extends DropDownList {			

		public var modeAll;

		//[Bindable]
		//public var nlabel = '';

		//[Bindable]
		//public var ldata : ArrayCollection = new ArrayCollection();
		//public var ldata : ArrayCollection;

		
		//[Bindable] public var bitPart = 0;
		//public var chanComb:Function; 
		
		
		

		private var out;
		private var label0;
		private var maxch = 8;
		
		
		//fac
		var factory:ClassFactory = new ClassFactory(renderOneCheck);
		
		
		//*************************************
		
		public function CheckCombo(){
			super();
			//ldata = new ArrayCollection();
			//dataProvider.addEventListener(CollectionEvent.COLLECTION_CHANGE, chi);
			itemRenderer = factory;
			label0 = modeAll ? FlexGlobals.topLevelApplication.la.all : FlexGlobals.topLevelApplication.la.no;
			//dataProvider = ldata;
			//restrict = '♠';
			//addEventListener("creationComplete", function(e){if (dataProvider && !dataProvider.length) dataProvider = FlexGlobals.topLevelApplication.lPartName;  });
			addEventListener("change", function(e){
					 if (dataProvider)
				 		if (selectedIndex != 0) dataProvider.getItemAt(0).ch_ = false; 
			 });
		}
		
		
		override protected function focusInHandler(event : FocusEvent) : void {
			super.focusInHandler(event);
			out = false;
		}


		override protected function focusOutHandler(event : FocusEvent) : void {
			super.focusOutHandler(event);
			out = true;
			dropDownController_closeHandler(new DropDownEvent('close')); 
		}
		
		
		override protected function dropDownController_closeHandler(event : DropDownEvent) : void {
				setc(null);		
				//Alert.show(focusManager.getFocus());
				if(out) 
				super.dropDownController_closeHandler(event);
				//if (chanComb) chanComb();
		}
		
		override public function closeDropDown(commit : Boolean) : void {
			super.closeDropDown(commit);	
			//Alert.show('ss');
		}
		
		
		
		override public function set dataProvider(value : IList) : void{
			
				super.dataProvider = value;
				
				setc(null);
				
		}
			
	
				
			
		override mx_internal function updateLabelDisplay(displayItem : * = undefined) : void{
			
				//super.updateLabelDisplay(displayItem);
				/*
				setTimeout(function(){	
					var s = '';
					for (var i = 0; i<ldata.length; i++){
							if (ldata.getItemAt(i).ch_)  s += (i+1) + ',';
							//Alert.show(ldata.getItem(i));
					}
					textInput.text = s.replace(',$','');
				},1000);*/
				//Alert.show(displayItem);
				
				//if (dataProvider)
					//if (selectedIndex != 0) dataProvider.getItemAt(0).ch_ = false;
		}


		public function closeDropForse(){
			setc(null);
			out = true;
			dropDownController_closeHandler(new DropDownEvent('close')); 	
		}


		public function forseClose(){
			
			if (isDropDownOpen){
				out = true;
				dropDownController_closeHandler(new DropDownEvent('close'));
			} 
			
		}
		
		public function setc(e) {
			
			if (dataProvider && dataProvider.length) {
				
				
				if (dataProvider.getItemAt(0).ch_ == true){
					
				//Alert.show(FlexGlobals.topLevelApplication.la.no);
				
					for (var i = 1; i < dataProvider.length; i++) 
								dataProvider.getItemAt(i).ch_ = modeAll;
								
					labelDisplay.text = label0;
					return;			
				}
				
				var s = '';
				var n = 0;
				for (i = 1; i < dataProvider.length; i++) {
					if (dataProvider.getItemAt(i).ch_){  
						if (n < maxch){
							 s += (i) + ',';
						} else
							dataProvider.getItemAt(i).ch_ = false;
						n++;
					}	
								//Alert.show(ldata.getItem(i));
				}
				//setTimeout(function(){Alert.show(bitPart)}, 1000);
				//textInput.text = s.substr(0, s.length - 1);				
				if (!s){
				  dataProvider.getItemAt(0).ch_ = true;
				  s = label0;
				} 
				labelDisplay.text = s.substr(0, s.length - 1);
				//dispatchEvent(new CollectionEvent('collectionChange'));
			}
		}
	}
}